package com.achilles.learnasyncqueryhandler;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by AchillesL on 2016/6/16.
 */
public class ListViewAdapter extends BaseAdapter {

    private List<ContactsDataModel> mContactsDataModels;
    private Context mContext;

    public ListViewAdapter(Context mContext) {
        this.mContext = mContext;
        mContactsDataModels = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return mContactsDataModels.size();
    }

    @Override
    public Object getItem(int position) {
        return mContactsDataModels.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ContactsDataModel contactsDataModel = (ContactsDataModel) getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_listview, parent,
                    false);
        }

        TextView tvContactsName = (TextView) convertView.findViewById(R.id.tvContactsName);
        TextView tvPhoneNumber = (TextView) convertView.findViewById(R.id.tvPhoneNumber);

        tvContactsName.setText(contactsDataModel.getContactsName());
        tvPhoneNumber.setText(contactsDataModel.getPhoneNumber());

        return convertView;
    }

    public void updateDatas(List<ContactsDataModel> contactsDataModels) {
        this.mContactsDataModels = contactsDataModels;
        this.notifyDataSetChanged();
    }
}
